package com.ptszyxx.popose.module.main.tea.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class TeaReleaseVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> name = new ObservableField();
    public ObservableField<String> person = new ObservableField();
    public ObservableField<String> num = new ObservableField();
    public ObservableField<String> address = new ObservableField();
    public ObservableField<String> time = new ObservableField();
    public ObservableField<String> heat = new ObservableField();
    public ObservableField<String> sweet = new ObservableField();
    public ObservableField<String> categorize = new ObservableField();

    public TeaReleaseVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onPersonEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onTimeEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onHeatEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onSweetEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onCategorizeEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onReleaseCommand = new BindingCommand(()-> onSubmit());
    public BindingCommand onPersonCommand = new BindingCommand(()-> uc.onPersonEvent.call());
    public BindingCommand onTimeCommand = new BindingCommand(()-> uc.onTimeEvent.call());
    public BindingCommand onHeatCommand = new BindingCommand(()-> uc.onHeatEvent.call());
    public BindingCommand onSweetCommand = new BindingCommand(()-> uc.onSweetEvent.call());
    public BindingCommand onCategorizeCommand = new BindingCommand(()-> uc.onCategorizeEvent.call());

    /**
     */
    public void onSubmit(){
        if (YStringUtil.isEmpty(name.get())){
            YToastUtil.showShort(R.string.tea_release_name_hint);
            return;
        }
        if (YStringUtil.isEmpty(person.get())){
            YToastUtil.showShort(R.string.tea_release_person_hint);
            return;
        }
        if (YStringUtil.isEmpty(num.get())){
            YToastUtil.showShort(R.string.tea_release_num_hint);
            return;
        }
        if (YStringUtil.isEmpty(address.get())){
            YToastUtil.showShort(R.string.tea_release_address_hint);
            return;
        }
        if (YStringUtil.isEmpty(time.get())){
            YToastUtil.showShort(R.string.tea_release_time_hint);
            return;
        }
        if (YStringUtil.isEmpty(heat.get())){
            YToastUtil.showShort(R.string.tea_release_heat_hint);
            return;
        }
        if (YStringUtil.isEmpty(sweet.get())){
            YToastUtil.showShort(R.string.tea_release_sweet_hint);
            return;
        }
        if (YStringUtil.isEmpty(categorize.get())){
            YToastUtil.showShort(R.string.tea_release_categorize_hint);
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("teaName", name.get());
        map.put("teaObjectName", person.get());
        map.put("teaRenNum", num.get());
        map.put("teaPlace", address.get());
        map.put("teaTime", time.get());
        map.put("teaWenDu", heat.get());
        map.put("teaTianDu", sweet.get());
        map.put("teaHuoDongType", categorize.get());

        HttpUtils.getInstance().data(model.teaSave(map), this, new OnSuccessResult() {
            @Override
            public void onSuccessResult(BaseResponse response) {
                YToastUtil.showShort("保存成功");
                YActivityUtil.getInstance().close(TeaReleaseVM.this);
            }
        });
    }




}
